package com.atguigu.ssyx.acl.server.impl;

import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.server.AdminRoleService;
import com.atguigu.ssyx.acl.server.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * Created with IntelliJ IDEA.
 * ClassName: RoleServiceImpl
 * Package: com.atguigu.ssyx.acl.server.impl
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-09-19
 * Time: 19:51
 */

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private AdminRoleService adminRoleService;

    //角色条件分页查询
    @Override
    public IPage<Role> selectRolePage(Page<Role> rolePage, RoleQueryVo roleQueryVo) {
        //判断参数是否为空 封装成了对象
        String roleName = roleQueryVo.getRoleName();
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)) {
            //不为空 查询所有
            //模糊条件
            wrapper.like(Role::getRoleName, roleName);
        }
        return baseMapper.selectPage(rolePage, wrapper);
    }

    //分配角色
    //查询所有角色 和 根据用户id查询用户分配角色列表
    //返回Map集合 刚好是对应的Key-Val
    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {
        //1.查询所有的角色
        List<Role> allRolesList = baseMapper.selectList(null);
        //2.根据用户ID查询用户分配角色列表
        //2.1 先查关系表 List<Role>
        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRole::getAdminId, adminId);
        List<AdminRole> listAdminRole = adminRoleService.list(queryWrapper);
        //2.2 查询出来的关系表得到的是对象集合 再从对象结合抽取RoleId出来List<Long>
        List<Long> collect =
                listAdminRole.stream().map(item ->
                        item.getRoleId()).collect(Collectors.toList());
        //2.3 创建新的List集合 用户存储用户分配的角色
        List<Role> assignRoleList = new ArrayList<>();
        //2.4 判断角色里面是否包含已经分配的角色id 封装到2.3的List中
        for (Role role : allRolesList) {
            //判断
            if (collect.contains(role.getId())){
                assignRoleList.add(role);
            }
        }
        //最后是以Map返回的
        Map<String, Object> result = new HashMap<>();
        result.put("allRolesList", allRolesList);

        //使用SQL语句实现 一句sql实现上面代码
        List<Role> sqlRoleList = baseMapper.selectAdminIdByRoleId(adminId);

        //result.put("assignRoles",assignRoleList);
        result.put("assignRoles",sqlRoleList);

        return result;

    }

    //为用户分配角色
    @Override
    public void saveAdminRole(Long adminId, Long[] roleIds) {
        //1.删除改用户已经分配的角色数据
        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRole::getAdminId,adminId);
        adminRoleService.remove(queryWrapper);
        //2.重新分配
        //遍历多个角色id 得到每个角色id 拿着每个角色id + adminId添加到关系表中
        AdminRole adminRole = new AdminRole();
        adminRole.setAdminId(adminId);
        for (Long roleId : roleIds) {
            adminRole.setRoleId(roleId);
            //这里调用几次就添加几次
            adminRoleService.save(adminRole);
            //可以又一个改进的方法 将角色id + adminId封装为List<AdminRole>
            //然后调用saveBatch 一次直接添加 批量添加
        }
    }
}
